package com.jhws.business.customer.service;

import com.jhws.business.comm.bo.TblCommDimBo;
import com.jhws.business.comm.service.ITblCommDimService;
import com.jhws.business.customer.bo.CustConstants;
import com.jhws.business.customer.bo.PayCustomer;
import com.jhws.business.customer.bo.PayCustomerSecret;
import com.jhws.common.pay.bo.PayConstants;
import com.jhws.core.db.BaseDao;
import com.jhws.sys.user.bo.TblUserDimBo;
import com.jhws.sys.user.service.ITblUserDimService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class PayCustService {

    @Resource
    BaseDao BaseDao;

    public List<PayCustomer> findPayCustomers(){
        return BaseDao.findAll("PayCustomer");
    }

    public List<PayCustomer> findPayCustByWhere(String where){
        return BaseDao.findList("from PayCustomer where "+where);
    }

    public Map findPayCustomersPage(){
        return BaseDao.queryPage("from PayCustomer");
    }

    public Map findCustomerSecretsPageByWhere(String where){
        return BaseDao.queryPage("from PayCustomerSecret where "+where);
    }

    public PayCustomerSecret getCustomerSecretById(Long id){
        return BaseDao.get(PayCustomerSecret.class,id);
    }

    /**
     * 查找申请支付宝代签的商户信息
     *
     * 将申请账号的userCode放到appId中带下去了，反正appId不需要,避免申请新的bean
     * @return
     */
    public Map findCustomersSecretsApplyingPage(){
        return BaseDao.queryPage("select new PayCustomerSecret(s.id,s.mchId,user.userCode,user.userName,s.time) from PayCustomerSecret as s,TblUserDimBo as user where " +
                "s.state = '"+ CustConstants.PAY_SECRET_STATE_APPLY+"' and s.creator = user.logName");
    }

    public List<PayCustomerSecret> findCustomerSecretsByWhere(String where){
        return BaseDao.findList("from PayCustomerSecret where "+where);
    }

    public Map findPayCustomerSecrets(Long customId){
        return BaseDao.queryPage("from PayCustomerSecret where customId = '"+customId+"'");
    }

    public PayCustomerSecret getPayCustSecretById(Long secretId){
        return BaseDao.get(PayCustomerSecret.class,secretId);
    }

    public List<PayCustomerSecret> getPayCustSecretByWhere(String where){
        return BaseDao.findList("from PayCustomerSecret where "+where);
    }

    public PayCustomer getPayCustomerById(Long id){
        return BaseDao.get(PayCustomer.class,id);
    }

    public PayCustomer getPayCustomerByAccount(String user){
        List<PayCustomer> payCustomerList = BaseDao.findList("from PayCustomer where creator = '"+user+"'");
        if(payCustomerList == null || payCustomerList.size() == 0)
            return null;

        return payCustomerList.get(0);
    }

    public <T> void save(T customer){
        BaseDao.saveOrUpdate(customer);
    }

    public void delete(Long id){
        BaseDao.delete(PayCustomer.class,id);
    }

    public void deleteDetail(Long id){
        BaseDao.delete(PayCustomerSecret.class,id);
    }

    public void deleteDetailByCustomId(Long customId){
        BaseDao.deleteByColumns("PayCustomerSecret","customId",customId+"");
    }

    public PayCustomerSecret findCustSecretByPayType(Long customId, String payType) throws Exception{
        List<PayCustomerSecret> list =  BaseDao.findList("from PayCustomerSecret where customId = '"+customId+"' and type = '"+payType+"'");
        return list != null && list.size()!=0?list.get(0):null;
    }

    public PayCustomerSecret findCustSecretByAppId(String appId){
        List<PayCustomerSecret> list =  BaseDao.findList("from PayCustomerSecret where appId = '"+appId+"'");
        return list != null && list.size()!=0?list.get(0):null;
    }

    public PayCustomer saveNewCustomForUser(TblUserDimBo user){
        PayCustomer payCustomer = new PayCustomer();
        payCustomer.setCreator(user.getLogName());
        payCustomer.setCustomName(user.getUserName());
        payCustomer.setCustomDetail(user.getRoleName()+"角色用户所创建的");
        save(payCustomer);
        return payCustomer;
    }
}
